//
//  SecondTabHotViewController.m
//  项目A --半糖
//
//  Created by romana on 16/6/1.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SecondTabHotViewController.h"
#import "SecondTabHotCollectionViewCell.h"
#import "SecondTabHotModel.h"
#import "SecondTabHotDetailsViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "DataBaseManager.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SecondTabHotViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation SecondTabHotViewController

-(void)viewWillAppear:(BOOL)animated{
    //    self.dataArr = [[DataBaseManager shareIndtance] selectAllHot];
    [self.collectionView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.dataArr = [NSMutableArray array];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    // 导航栏的设置
    [self designNavigationBar];
    // 创建collectionView
   [self createCollectionView];
    //请求数据
    [self getData];
    
    // 收藏数据库
    [[DataBaseManager shareIndtance] openHotDB];
    [[DataBaseManager shareIndtance] createHotTable];
    
    // Do any additional setup after loading the view.
}



-(void)getData{

    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?limit=20&offset=0&gender=2&generation=2"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        NSArray *arr = dataDic[@"items"];
        
        for (NSDictionary *tempDic in arr) {
            NSDictionary *dic = tempDic[@"data"];
            SecondTabHotModel *h= [[SecondTabHotModel alloc] init];
            [h setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:h];
        }
        
        [self.collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];



}

-(void)designNavigationBar{
   // self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:218 / 255.0 green:82 / 255.0 blue:146 / 255.0 alpha:1];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"热门";
    self.navigationItem.titleView = titleLabel;
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((WIDTH - 20 - 10) / 2, (HEIGHT - 64 - 49 - 30) / 2 - 10);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[SecondTabHotCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return  self.dataArr.count;


}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondTabHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    SecondTabHotModel *h = self.dataArr[indexPath.row];
    
    cell.nameLable.text = h.name;
    [cell.nameLable sizeToFit];
    cell.priceLable.text = h.price;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:h.cover_image_url]];
    cell.likeLable.text = [NSString stringWithFormat:@"%@",h.favorites_count];
    
    cell.likeBtn.selected = [[DataBaseManager shareIndtance] selectHotByTitle:h.name];
    cell.url = h.url;
    cell.type = @"items";
    return cell;


}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    SecondTabHotDetailsViewController *detailVC = [[SecondTabHotDetailsViewController alloc] init];
    SecondTabHotModel *h = self.dataArr[indexPath.row];
    detailVC.myUrl = h.url;
    detailVC.type = @"items";
    [self.navigationController pushViewController:detailVC animated:YES];



}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
